package br.com.chat.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ScrollPaneConstants;

import br.com.chat.controller.impl.ClienteChatImpl;
import br.com.chat.model.ChatVO;
import br.com.chat.model.Cliente;

public class ChatView {

	private JFrame frame;
	private Cliente cliente;
	private JTextArea entrada;
	private JTextField saida;
	private JList<Cliente> listContatos;
	private ChatVO chatVO;
	private ClienteChatImpl clienteChat = new ClienteChatImpl();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ChatView window = new ChatView();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ChatView(Cliente cliente) {
		this.cliente = cliente;
		initialize();
	}
	
	public ChatView() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		this.chatVO = new ChatVO();
		JFrame frame = new JFrame("MEU PRIMEIRO CHAT");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel painelPrincipal = new JPanel();
		entrada = new JTextArea(15, 50);
		entrada.setLineWrap(true);
		entrada.setWrapStyleWord(true);
		entrada.setEditable(false);
		JScrollPane qScroller = new JScrollPane(entrada);
		qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		saida = new JTextField(20);
		
		JButton envia = new JButton("Enviar");
		envia.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				chatVO.setMensagemEntrada(entrada.getText());
				chatVO.setEntrada(entrada);
				chatVO.setMensagemSaida(saida.getText());
				clienteChat.sendChatVO(chatVO);
				saida.setText("");
				saida.requestFocus();	
			}
		});
		frame.getContentPane().add(painelPrincipal, BorderLayout.NORTH);
		
		listContatos = new JList<Cliente>();
//		listContatos.setListData((Cliente[]) chatVO.getListaContatos().toArray());
		JLabel lblNewLabel_1 = new JLabel(cliente.getUsuario().getNickname());
		GroupLayout gl_painelPrincipal = new GroupLayout(painelPrincipal);
		gl_painelPrincipal.setHorizontalGroup(
			gl_painelPrincipal.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_painelPrincipal.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_painelPrincipal.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_painelPrincipal.createSequentialGroup()
							.addGap(43)
							.addComponent(saida, GroupLayout.PREFERRED_SIZE, 303, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(envia, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_painelPrincipal.createSequentialGroup()
							.addComponent(lblNewLabel_1)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(qScroller, GroupLayout.PREFERRED_SIZE, 423, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(listContatos, GroupLayout.PREFERRED_SIZE, 212, GroupLayout.PREFERRED_SIZE)
					.addGap(19))
		);
		gl_painelPrincipal.setVerticalGroup(
			gl_painelPrincipal.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_painelPrincipal.createSequentialGroup()
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(gl_painelPrincipal.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_painelPrincipal.createSequentialGroup()
							.addComponent(listContatos, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addContainerGap())
						.addGroup(Alignment.TRAILING, gl_painelPrincipal.createSequentialGroup()
							.addGroup(gl_painelPrincipal.createParallelGroup(Alignment.LEADING)
								.addComponent(qScroller, GroupLayout.PREFERRED_SIZE, 264, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 262, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
							.addGroup(gl_painelPrincipal.createParallelGroup(Alignment.BASELINE)
								.addComponent(envia)
								.addComponent(saida, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(59))))
		);
		painelPrincipal.setLayout(gl_painelPrincipal);
		frame.setSize(755,356);
		frame.setVisible(true);
	}
}
